package cn.pw.test.com.conversion.fragment;

import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.widget.TextView;

import cn.pw.test.com.conversion.R;
import cn.pw.test.com.conversion.view.ConversionInputView;
import tech.com.commoncore.base.BaseFragment;
import tech.com.commoncore.utils.DataUtils;
import tech.com.commoncore.utils.ToastUtil;

/**
 * 原油换算
 */
public class ConversionCrudeFragment extends BaseFragment {

    private ConversionInputView inMOPSPrice, inStsPrice, inTariff, inVAT, inOther, inExchangeRate, inResult1;

    private TextView calculationTx, clearTx;

    public static ConversionCrudeFragment newInstance() {
        ConversionCrudeFragment fragment = new ConversionCrudeFragment();
        return fragment;
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_conversion_crude;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        inMOPSPrice = mContentView.findViewById(R.id.conversion_input_MOPS_price);
        inStsPrice = mContentView.findViewById(R.id.conversion_input_sts_price);
        inTariff = mContentView.findViewById(R.id.conversion_input_tariff);
        inVAT = mContentView.findViewById(R.id.conversion_input_VAT);
        inOther = mContentView.findViewById(R.id.conversion_input_other);
        inExchangeRate = mContentView.findViewById(R.id.conversion_input_exchange_rate);
        inResult1 = mContentView.findViewById(R.id.conversion_input_result1);
        calculationTx = mContentView.findViewById(R.id.conversion_button_positive);
        clearTx = mContentView.findViewById(R.id.conversion_button_negative);

        calculationTx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calculation();
            }
        });

        clearTx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cleanUp(inMOPSPrice, inOther, inStsPrice, inTariff, inVAT, inExchangeRate, inResult1);
            }
        });


        View decorView = getActivity().getWindow().getDecorView();
        View contentView = getActivity().findViewById(Window.ID_ANDROID_CONTENT);
        decorView.getViewTreeObserver().addOnGlobalLayoutListener(getGlobalLayoutListener(decorView, contentView));

    }

    private ViewTreeObserver.OnGlobalLayoutListener getGlobalLayoutListener(final View decorView, final View contentView) {
        return new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                decorView.getWindowVisibleDisplayFrame(r);

                int height = decorView.getContext().getResources().getDisplayMetrics().heightPixels;
                int diff = height - r.bottom;

                if (diff != 0) {
                    if (contentView.getPaddingBottom() != diff) {
                        contentView.setPadding(0, 0, 0, diff);
                    }
                } else {
                    if (contentView.getPaddingBottom() != 0) {
                        contentView.setPadding(0, 0, 0, 0);
                    }
                }
            }
        };
    }


    private void calculation() {
        String etMops = inMOPSPrice.getInputEd().getText().toString().trim();
        String etShengtieshui = inStsPrice.getInputEd().getText().toString().trim();
        String etTariff = inTariff.getInputEd().getText().toString().trim();
        String etVatRate = inVAT.getInputEd().getText().toString().trim();
        String etOtherFee = inOther.getInputEd().getText().toString().trim();
        String etExchangeRate = inExchangeRate.getInputEd().getText().toString().trim();

        if (DataUtils.isEmpty(etMops)) {
            ToastUtil.show(R.string.conversion_string_input_MOPS_hint);
            return;
        }


        if (DataUtils.isEmpty(etShengtieshui)) {
            ToastUtil.show(R.string.conversion_string_input_sts_hint);
            return;
        }

        if (DataUtils.isEmpty(etTariff)) {
            ToastUtil.show(R.string.conversion_string_input_tariff_hint);
            return;
        }

        if (DataUtils.isEmpty(etVatRate)) {
            ToastUtil.show(R.string.conversion_string_input_VatRate_hint);
            return;
        }

        if (DataUtils.isEmpty(etOtherFee)) {
            ToastUtil.show(R.string.conversion_string_input_OtherFee_hint);
            return;
        }

        if (DataUtils.isEmpty(etExchangeRate)) {
            ToastUtil.show(R.string.conversion_string_input_exchange_hint);
            return;
        }

        double mops = Double.valueOf(etMops);
        double shengtieshui = Double.valueOf(etShengtieshui);
        double tariff = Double.valueOf(etTariff);
        double vatRate = Double.valueOf(etVatRate);
        double otherFee = Double.valueOf(etOtherFee);
        double exchangeRate = Double.valueOf(etExchangeRate);

        //进口燃料油成本＝（MOPS价格＋升贴水）×汇率×（1＋关税税率）×（1＋增值税税率）＋其他费用
        double result = (mops + shengtieshui) * exchangeRate * (1 + tariff / 100) * (1 + vatRate / 100) + otherFee;
        String result1 = DataUtils.getAmountValue(result + "");
        setResult1(result1);
    }

    private void setResult1(String result1) {
        inResult1.getInputEd().setText(result1);
    }

    private void cleanUp(ConversionInputView... view) {
        for (ConversionInputView conversionInputView : view) {
            conversionInputView.getInputEd().setText("");
        }
    }
}
